package com.easyattendance.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.easyattendance.model.Holidays;
import com.easyattendance.model.Proxy;
import com.easyattendance.model.Subject;
import com.easyattendance.services.AddHolidayListService;
import com.easyattendance.services.AddProxyService;
import com.easyattendance.services.AddSectionService;

@WebServlet(name="AddProxyServlet",urlPatterns="/AddProxyServlet")
public class AddProxyServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
    
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    	response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		Date proxy_date = null;
		
		try {
			proxy_date = format.parse(request.getParameter("proxy_dt"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//String day_of_week=request.getParameter("day_selected");
		String timeslot=request.getParameter("timeslot_selected");
		String class_id=request.getParameter("class_select");
		String subject_teacher_id=request.getParameter("subject_teacher_id");
		String subject_id = request.getParameter("subject_id");
		String proxy_teacher_id=request.getParameter("proxy_teacher_select");
		String proxy_subject=request.getParameter("subject_select");
		String section_id=request.getParameter("section_id");
		
		 Proxy proxy=new Proxy(proxy_date,timeslot,subject_teacher_id,proxy_subject,subject_id,proxy_teacher_id,class_id,section_id);
		
		 
		 AddProxyService addProxyService=new AddProxyService();
		 boolean res=addProxyService.ifAlreadyExists(proxy);
		
		 boolean result=false;
		 if(!res){
			 result=addProxyService.addProxy(proxy);	 
		 }
		 else {
			 	request.setAttribute("proxyExists", "Proxy is already allotted");
	        	RequestDispatcher rd = request.getRequestDispatcher("views/section/generalSettings.jsp");
	        	rd.forward(request, response);
		 }
		
		 if(result){
			request.setAttribute("proxyAdded", "Proxy added Successfully.");
	        RequestDispatcher rd = request.getRequestDispatcher("views/section/generalSettings.jsp");
	        rd.forward(request, response);
		} else {
				request.setAttribute("proxyNotAdded", "Proxy not added successfully.");
	        	RequestDispatcher rd = request.getRequestDispatcher("views/section/generalSettings.jsp");
	        	rd.forward(request, response);
		}
		 
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
